package com.thomas.framework.service.impl;

import com.thomas.framework.core.AppException;
import com.thomas.framework.core.ServiceBase;
import com.thomas.framework.dao.ISysUserDao;
import com.thomas.framework.entity.SysModel;
import com.thomas.framework.entity.SysUser;
import com.thomas.framework.service.ISysModelService;
import com.thomas.framework.service.ISysUserService;
import com.thomas.framework.utils.CommonUtil;
import com.thomas.framework.utils.MD5Util;
import com.thomas.framework.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * Created by taohj on 2014/4/20.
 */

@Service

public class SysUserServiceImpl extends ServiceBase<SysUser> implements ISysUserService {

    @Autowired
    private ISysUserDao dao;


    @Autowired
    private ISysModelService modelService;





    public boolean create(SysUser user) {

        SysUser validataUser = new SysUser();
        validataUser.setUserName(user.getUserName());
        validataUser = dao.select(validataUser);
        if (validataUser != null) {
            throw new AppException("There has same username,please type othere");
        }
        user.setId(CommonUtil.getUUID());
        if (!StringUtils.hasLength(user.getPassword())) user.setPassword("123456");
        if (!StringUtils.hasLength(user.getStatus())) user.setStatus("1");
        user.setRegistrationTime(new Date());
        user.setHashCode(MD5Util.MD5Code(user.getUserName() + user.getPassword()));
        return dao.insert(user) > 0;
    }

    public boolean remove(SysUser user) {
        return dao.delete(user) > 0;
    }

    public boolean modify(SysUser user) {
        return dao.update(user) > 0;
    }


    public SysUser getById(SysUser user) {

        return dao.selectById(user);
    }

    @Override
    public List<SysUser> getList(SysUser user) {
        return dao.selectList(user);
    }

    public PageResult<SysUser> getByPage(PageResult<SysUser> pageResult) {
        pageResult.setRows(dao.selectbyPage(pageResult));
        return pageResult;
    }

    @Override
    public SysUser get(SysUser user) {
        return dao.select(user);
    }


    public SysUser login(SysUser user) {


        if (context.getAdmin().getUserName().equals(user.getUserName())) {
            if (user.getPassword().equals(context.getAdmin().getPassword())){
                user= context.getAdmin();
            }else{
                return  null;
            }
        }else {
            user = dao.login(user);
        }


       if (user==null){
           return  null;
       }

         initUserFunctions(user);

        return user;

    }


    private void initUserFunctions(SysUser user){

        if (context.getAdmin().getUserName().equals(user.getUserName())){
            List<SysModel> list = new ArrayList<SysModel>();
            Map<String, SysModel> functions = new HashMap<String, SysModel>();
            List<SysModel> _functions = modelService.getFunctionByAdmin();
            List<SysModel> _functionMenus = modelService.getMenuByAdmin();
            list.addAll(_functions);
            list.addAll(_functionMenus);
            for (SysModel m : list) {
                functions.put(m.getText(), m);
            }
            user.set_functionMenus(_functionMenus);
            user.set_functions(_functions);
            user.setFunctions(functions);
        }else{

            List<SysModel> list = new ArrayList<SysModel>();
            Map<String, SysModel> functions = new HashMap<String, SysModel>();
            List<SysModel> _functions = modelService.getFunctionByUser(user);
            List<SysModel> _functionMenus = modelService.getMenuByUser(user);
            list.addAll(_functions);
            list.addAll(_functionMenus);
            for (SysModel m : list) {
                functions.put(m.getText(), m);
            }
            user.set_functionMenus(_functionMenus);
            user.set_functions(_functions);
            user.setFunctions(functions);
        }


    }

    public boolean assignedRoles(SysUser user, SysUser currentUser) {
        dao.revocationRoles(user);
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("user", user);
        params.put("currentUser", currentUser);
        params.put("createTime", new Date());
        return dao.assignedRoles(params) > 0;
    }
}
